/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityBeans;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.Temporal;

/**
 *
 * @author h09577
 */
@NamedQueries({
    @NamedQuery(name = "ThreadEntity.getThreadBySubject", query = "SELECT t "
    + "FROM ThreadEntity t "
    + "WHERE t.mySubject=:mySubject"),
    @NamedQuery(name = "ThreadEntity.getMyReplies", query = "SELECT r "
    + "FROM ReplyEntity r "
    + "WHERE r.myThread=:myThread "
        + "ORDER BY r.myDate"),
    @NamedQuery(name = "ThreadEntity.threadByUuid", 
        query =     "SELECT t FROM ThreadEntity t"
                    + " WHERE t.uuid = :myUuid"
                     + " ORDER BY t.id")
})
@Entity
public class ThreadEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Basic
    private String uuid;
    @Temporal(javax.persistence.TemporalType.DATE)
    @Basic
    private Date myDate;
    @Basic
    private String title;
    @OneToMany(mappedBy="myThread", fetch= FetchType.EAGER, cascade = CascadeType.ALL)
    private List<ReplyEntity> myReplies;
    @ManyToOne
    private UserEntity myAuthor;
    @ManyToOne
    private SubjectEntity mySubject;

    
    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
    public SubjectEntity getMySubject() {
        return mySubject;
    }

    public void setMySubject(SubjectEntity mySubject) {
        this.mySubject = mySubject;
    }

    public List<ReplyEntity> getMyReplies() {
        return myReplies;
    }

    public void setMyReplies(List<ReplyEntity> myReplies) {
        this.myReplies = myReplies;
    }

    public UserEntity getMyAuthor() {
        return myAuthor;
    }

    public void setMyAuthor(UserEntity myAuthor) {
        this.myAuthor = myAuthor;
    }

    /**
     * Get the value of title
     *
     * @return the value of title
     */
    public String getTitle() {
        return title;
    }

    /**
     * Set the value of title
     *
     * @param title new value of title
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * Get the value of date
     *
     * @return the value of date
     */
    public Date getMyDate() {
        return myDate;
    }

    /**
     * Set the value of date
     *
     * @param date new value of date
     */
    public void setMyDate(Date myDate) {
        this.myDate = myDate;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }
    
    @PrePersist
    public void initialise(){
        this.setMyDate(new Date());
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof ThreadEntity)) {
            return false;
        }
        ThreadEntity other = (ThreadEntity) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entityBeans.ThreadEntity[ id=" + id + " ]";
    }
    
    //Ajoute un post à la conversation
    public void addReply(entityBeans.ReplyEntity reply)
    {
        this.myReplies.add(reply);
    } 
    
}
